package com.android.delivery.dialog;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.android.delivery.App;
import com.android.delivery.R;
import com.android.delivery.api.ApiService;
import com.android.delivery.callback.DialogCallback;
import com.android.delivery.callback.LzyResponse;
import com.android.delivery.constant.Constants;
import com.android.delivery.entity.LoginInfo;
import com.android.delivery.entity.PayWxInfo;
import com.android.delivery.util.pay.PayCommon;
import com.android.delivery.util.pay.WXPayCommon;
import com.blankj.utilcode.util.ToastUtils;
import com.lzy.okgo.model.Response;


/**
 * Desc:支付弹框
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class PayMethodDialog extends DialogFragment implements View.OnClickListener {

    private OnPayMethodDialogListener mOnGuardDialogListener;

    public void setOnGuardDialogListener(OnPayMethodDialogListener listener) {
        this.mOnGuardDialogListener = listener;
    }

    private String mOrderNo;

    public static PayMethodDialog newInstance(String orderNo) {
        PayMethodDialog payMethodDialog = new PayMethodDialog();
        Bundle bundle = new Bundle();
        bundle.putString(Constants.ORDER_ID, orderNo);
        payMethodDialog.setArguments(bundle);
        return payMethodDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCancelable(true);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(true);
        Window window = getDialog().getWindow();
        window.getAttributes().windowAnimations = R.style.PopAnimBottom;
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.popup_pay, container, false);
        dialogView.findViewById(R.id.tv_alipay).setOnClickListener(this);

        TextView tvBalance = dialogView.findViewById(R.id.tv_balance);
        tvBalance.setOnClickListener(this);
        View lineBalance = dialogView.findViewById(R.id.line_balance);

        dialogView.findViewById(R.id.tv_weicha).setOnClickListener(this);
        dialogView.findViewById(R.id.tv_cancel).setOnClickListener(this);

        LoginInfo loginInfo = App.Companion.getInstance().getLoginInfo();
        if (loginInfo != null && loginInfo.getRole() == 2){
            tvBalance.setVisibility(View.GONE);
            lineBalance.setVisibility(View.GONE);
        }else {
            tvBalance.setVisibility(View.VISIBLE);
            lineBalance.setVisibility(View.VISIBLE);
        }

        initData();
        return dialogView;
    }

    private void initData() {
        Bundle bundle = getArguments();
        if (bundle != null){
            mOrderNo = bundle.getString(Constants.ORDER_ID);
        }
        Log.e(PayMethodDialog.class.getSimpleName(), "initData:"+getActivity());
    }

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, PayMethodDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        if (viewId == R.id.tv_cancel) {
            dismiss();
        } else if (viewId == R.id.tv_alipay) {
            aliPay();
            dismiss();
        } else if (viewId == R.id.tv_weicha) {
            if (mOnGuardDialogListener != null){
                mOnGuardDialogListener.onWxPay(mOrderNo);
            }
            dismiss();
        }else if (viewId == R.id.tv_balance){
            if (mOnGuardDialogListener != null){
                mOnGuardDialogListener.onBalancePay(mOrderNo);
            }
            dismiss();
        }
    }

    private void aliPay() {
        PayCommon payCommon = new PayCommon(getActivity(), new PayCommon.payListener() {
            @Override
            public void paySucc() {
            }

            @Override
            public void payFail() {

            }
        });
//        payCommon.pay(orderInfo.orderStr);
    }

    public interface OnPayMethodDialogListener {
        void onWxPay(String orderNo);

        void onBalancePay(String orderNo);

        void selectPic();
    }
}

